<?
    /* -*- Mode: PHP5; tab-width: 4; indent-tabs-mode: nil; basic-offset: 4 -*- */

    /*********************************************************
    *            --== InArch ==--
    *
    * @author Kulikov Alexey <a.kulikov@gmail.com>
    * @version n/a
    * @since 04.05.2005
    * @copyright essentialmind.com 2005
    *
    *********************************************************/
    
    /***
    * Class botImage
    ****
    * returns a 'bot-check' image to the browser, must be called
    * before any output is sent to the browser.
    ****
    * @package usefulStuff
    * @author A Kulikov <a.kulikov@gmail.com>
    * @version n/a
    * @since 04.05.2005
    * @copyright essentialmind.com 2005
    ***/
    class botImage{
        /**
         * Literal names of available digits
         *
         * @var unknown
         */
        private $digitNames;
        
        
        /**
         * Constructor
         *
         */
        function __construct(){
            $this->digitNames = array(
                                    1=>1,
                                    2=>2,
                                    3=>3,
                                    4=>4,
                                    5=>5,
                                    6=>6,
                                    7=>7,
                                    8=>8,
                                    9=>9,
                                    0=>0
                                     );
        }
        
        
        /**
         * Returns a fresh confirmation image to the browser
         *
         * @param integer $number
         */
        function getImage($number=null){
            
            //check if the numer was set, if not, randomize it
            if(!isset($number) or (int)$number < 1000){
                $number = rand(1000,9999);
            }
            
            //send the number to a session variable
            $_SESSION['botNumber'] = $number;
            
            
            //split the number into its composites
            $thousands  = floor($number / 1000);
            $hundredth  = floor(($number % 1000) / 100);
            $tenth      = floor(($number % 100) / 10);
            $digits     = $number % 10;
            
            //temporarily create the whole sting, this is neccessary in order
            //to calculate the overall image width
            $verifyAsString = $this->digitNames[$thousands].','.$this->digitNames[$hundredth].','.$this->digitNames[$tenth].','.$this->digitNames[$digits];
      
            //calcualte various image dimensions  	
            $lengthTotal       = floor(strlen($verifyAsString) * 10);
            $lengthThousands   = floor(strlen($this->digitNames[$thousands].',') * 10);
            $lengthHundreds    = floor(strlen($this->digitNames[$hundredth].',') * 10);
            $lengthTens        = floor(strlen($this->digitNames[$tenth].',') * 10);
            
            //create the actual image
            $im = ImageCreate($lengthTotal,30);
            
            //set various colors
            $background_color = imagecolorallocate($im, rand(230,255), rand(230,255), rand(230,255));
            $bgtext_color     = imagecolorallocate($im, rand(230,255), rand(230,255), rand(230,255));
            $thousandsColor   = imagecolorallocate ($im, rand(0,255), 0, rand(0,255));
            $hundredthColor   = imagecolorallocate ($im, rand(0,155), rand(0,255), 0);
            $tensColor        = imagecolorallocate ($im, 0, 0, rand(0,255));
            $digitsColor      = imagecolorallocate ($im, rand(0,255), 0, rand(0,255));
        	
            //start output
            
            //create background fludge
            for($i=0;$i<20;$i++){
                imagestringup($im,3,$i*10+rand(1,10),25+rand(1,10),rand(33333,99999),imagecolorallocate($im, rand(200,255), rand(200,255), rand(200,255)));
            }
            
            //write the four digits on the image
        	imagestring($im, 5, 2, rand(1,13), $this->digitNames[$thousands].',', $thousandsColor);
        	imagestring($im, 5, $lengthThousands, rand(1,13), $this->digitNames[$hundredth].',', $hundredthColor);
        	imagestring($im, 5, $lengthThousands+$lengthHundreds, rand(1,13), $this->digitNames[$tenth].',', $tensColor);
        	imagestring($im, 5, $lengthThousands+$lengthHundreds+$lengthTens, rand(1,13), $this->digitNames[$digits], $digitsColor);
            
        	//send the image to the browser
        	Header("Content-Type: image/png");
        	ImagePNG($im);
        	ImageDestroy($im);       
        	
        	//terminate script =)))
        	exit;
        }    
    }
?>